/*************************************************************************
 * gOkey - A tile based game for Gnome.                                  *
 * Copyright (C) 2008  Onur Aslan  <onuraslan@gmail.com>                 *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

#include "gui.h"


GdkPixbuf *defaultPixbuf = NULL;

static void
free_tiles (void)
{
  int i;
  for (i = 0; COLOR_MAX * SIDE_TILE_MAX + 2; i++)
    g_object_unref (tilesPixbuf[i]);
}

void
configure_pixbufs (void)
{
  char *filename;
  int i, j, k, x, y;
  if (defaultPixbuf == NULL) {
    filename = g_build_filename (PIXMAPDIR, "default.png", NULL);
    defaultPixbuf = gdk_pixbuf_new_from_file (filename, NULL);
    g_free (filename);
  }
  /* first loading rack */
  if (rackPixbuf != NULL)
    g_object_unref (rackPixbuf); 
  rackPixbuf = gdk_pixbuf_new_subpixbuf (defaultPixbuf, 0, 0,
                                         RACKWIDTH, RACKHEIGHT);
  /* we'll only load one time and one for closed tile and joker */
  if (tilesPixbuf != NULL)
    free_tiles ();
  else
    tilesPixbuf = g_malloc (sizeof (defaultPixbuf) *
                            ( TILE_MAX / SIDE_MAX  + 1 ));
  y = RACKHEIGHT;
  k = 0;
  for (i = 0; i < COLOR_MAX; i++) {
    x = 0;
    for (j = 0; j < SIDE_TILE_MAX; j++) {
      tilesPixbuf[k] = gdk_pixbuf_new_subpixbuf (defaultPixbuf, x, y,
                                                 TILEWIDTH, TILEHEIGHT);
      x += TILEWIDTH;
      k++;
    }
    y += TILEHEIGHT;
  }
  /* the last two tile is close tile and joker */
  tilesPixbuf[k++] = gdk_pixbuf_new_subpixbuf (defaultPixbuf, 0, y,
                                               TILEWIDTH, TILEHEIGHT);
  tilesPixbuf[k] = gdk_pixbuf_new_subpixbuf (defaultPixbuf, TILEWIDTH, y,
                                             TILEWIDTH, TILEHEIGHT);
}

GdkPixbuf *
get_tile_pixbuf (Tile tile)
{
  if (tile.type == CLOSED_TILE)
    return tilesPixbuf[SIDE_TILE_MAX * COLOR_MAX];
  else if (tile.type == FAKE_JOKER)
    return tilesPixbuf[SIDE_TILE_MAX * COLOR_MAX + 1];
  else
    return tilesPixbuf[( tile.order - 1 ) + ( SIDE_TILE_MAX * tile.color )];
}
